package vip.bblog.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import vip.bblog.admin.dao.SystemRoleMenuMapper;
import vip.bblog.admin.dto.SystemRoleAddDTO;
import vip.bblog.admin.entity.SystemRole;
import vip.bblog.admin.dao.SystemRoleMapper;
import vip.bblog.admin.entity.SystemRoleMenu;
import vip.bblog.admin.service.SystemRoleMenuService;
import vip.bblog.admin.service.SystemRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.bblog.admin.service.SystemUserRoleService;
import vip.bblog.common.utils.BaseResult;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author yuyong
 * @since 2019-05-30
 */
@Service
public class SystemRoleServiceImpl extends ServiceImpl<SystemRoleMapper, SystemRole> implements SystemRoleService {

    @Autowired
    private SystemRoleMapper systemRoleMapper;

    @Autowired
    private SystemRoleMenuService systemRoleMenuService;

    /**
     * 添加角色
     *
     * @param roleAddDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult<SystemRoleAddDTO> add(SystemRoleAddDTO roleAddDTO) {
        //添加到角色表
        systemRoleMapper.insert(roleAddDTO);
        //添加到角色菜单（权限）表
        List<SystemRoleMenu> roleMenus = new ArrayList<>(10);
        List<Long> menuIds = roleAddDTO.getMenuIds();
        Long roleId = roleAddDTO.getId();
        menuIds.forEach(menuId -> {
            SystemRoleMenu roleMenu = SystemRoleMenu.builder()
                    .roleId(roleId)
                    .menuId(menuId)
                    .build();
            roleMenus.add(roleMenu);
        });
        systemRoleMenuService.saveBatch(roleMenus);
        return BaseResult.success();
    }

    /**
     * 批量删除，删除父类时候，最好把子菜单id也带过来
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult<Object> deleteBatch(List<Long> ids) {
        //删除角色
        systemRoleMapper.deleteBatchIds(ids);
        //删除角色菜单（权限）关联表
        QueryWrapper<SystemRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id", ids);
        systemRoleMenuService.remove(queryWrapper);
        return BaseResult.success();
    }

    /**
     * 条件分页查询
     *
     * @param page
     * @param systemRole
     * @return
     */
    @Override
    public BaseResult<Object> selectPageByExample(Page<SystemRole> page, SystemRole systemRole) {
        QueryWrapper<SystemRole> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(systemRole.getName())) {
            queryWrapper.like("name", "%" + systemRole.getName() + "%");
        }
        IPage<SystemRole> pageResult = systemRoleMapper.selectPage(page, queryWrapper);
        return BaseResult.successWithData(pageResult);
    }
}
